# image quality assessment
import numpy as np
# from lib import *
import os
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import uitls.mean_nz as mean_nz
import phoneImg


def uniformity_assessment(img): # high fre
    # normalizaiton
    # method-1
    img = img / np.mean(img)
    sample1 = img[0:-1:2, 0:-1:2]
    sample2 = img[1:-1:2, 0:-1:2]
    sample3 = img[0:-1:2, 1:-1:2]
    sample4 = img[1:-1:2, 1:-1:2]
    # 全局方差
    val = np.var(sample1)+np.var(sample2)+np.var(sample3)+np.var(sample4)

    # pixel by pixel
    val_pixel = uniformity_assessment_pixelbypixel(img)
    val_f = (val+val_pixel)/2
    return val_f, val, val_pixel

def uniformity_assessment_pixelbypixel(img):
    # 评估pixel与pixel之间的差异，而非全局情况
    # img = img / np.mean(img)
    kernel = np.array([
        [0.05, 0.2, 0.05],
         [0.2, -1, 0.2],
         [0.05, 0.2, 0.05]
    ])
    img_conved = cv2.filter2D(img, -1, kernel)
    result = np.mean(np.abs(img_conved))
    return result


def uniformity_assessment_multi_areas(img):
    # method-2: 5/9/135 areas assessment
    r, c = img.shape

    # 9点
    # val_record = np.zeros([3, 3])
    # loc_x = [r / 6, 3 * r / 6, 5 * r / 6]
    # loc_y = [c / 4, 2 * c / 4, 3 * c / 4]
    # rad = 50  # changeable, rad val

    # 135点
    val_record = np.zeros([15, 9])
    loc_x = [int(r/16)*(x+1) for x in range(15)]
    loc_y = [int(c/16)*(y+1) for y in range(9)]
    rad = 20  # changeable, rad val

    # img_show = img.copy()
    # img_show =
    for idx in range(len(loc_x)):
        for idy in range(len(loc_y)):
            img_roi = img[int(loc_x[idx])-rad:int(loc_x[idx])+rad, int(loc_y[idy])-rad:int(loc_y[idy])+rad]
            # img[int(loc_x[idx])-rad:int(loc_x[idx])+rad, int(loc_y[idy])-rad:int(loc_y[idy])+rad] = 255
            val_record[idx, idy] = uniformity_assessment(img_roi)[0]
            # img_show
    return val_record

def diff_frq_uniformity_assessment(img):
    # 分频后，高频均一性评估
    ratio = [0.33,0.33,0.33]   # 高中低频占比
    kernel_size_l = (121, 121)
    sigma_l = 71
    kernel_size_m = (7, 7)
    sigma_m = 5

    img_l = cv2.GaussianBlur(img * ratio[0], kernel_size_l, sigma_l)
    img_m = cv2.GaussianBlur(img * (ratio[0] + ratio[1]), kernel_size_m, sigma_m)
    # img_m = cv2.GaussianBlur(img * (ratio[1]), kernel_size_m, sigma_m)
    img_m = img_m - img_l
    img_h = img - img_m - img_l

    # val = uniformity_assessment(img_h) # 在高频数据上进行整体评估
    val = uniformity_assessment_multi_areas(img_h)  # 在高频数据上进行分区域评估
    return val


def dark_point(img):
    return len(np.find(img, 0))

def low_frq_uniformity_assessment(img):
    # method-2: 5/9 areas assessment
    r, c = img.shape
    val_record = np.zeros([3,3])
    loc_x = [r / 6, 3 * r / 6, 5 * r / 6]
    loc_y = [c / 4, 2 * c / 4, 3 * c / 4]
    rad = 50 # changeable, rad val
    for idx in range(len(loc_x)):
        for idy in range(len(loc_y)):
            img_roi = img[int(loc_x[idx])-rad:int(loc_x[idx])+rad, int(loc_y[idy])-rad:int(loc_y[idy])+rad]
            # img[int(loc_x[idx])-rad:int(loc_x[idx])+rad, int(loc_y[idy])-rad:int(loc_y[idy])+rad] = 255
            val_record[idx, idy] = mean_nz._mean_nz(img_roi)
    coef = val_record.min()/val_record.max()
    return coef

def phoneImgAssessment(img):
    coef, diffx, diffy = phoneImg.show_color_img(img)
    return coef, diffx, diffy

if __name__ == '__main__':
    csv = pd.read_csv('D:\DC\data\O3\R33.csv', header = None)
    csv = csv.to_numpy()
    val = uniformity_assessment_multi_areas(csv)
    print('end')